<?php
		header('Content-Type: application/json; charset=utf-8');
		//header("Content-Type: text/html; charset=UTF-8");
        class Savetangdai extends CI_Controller{
                function __construct()
                {
                        parent::__construct();
                }
	/**
	*haoduanzi
	
				private function get_data($url) {
					if($url !== false){
						$data = file_get_contents( $url );
					}
					$charset_pos = stripos($data,'charset');
					if($charset_pos) {
						if(stripos($data,'charset=utf-8',$charset_pos)) {
							$data = iconv('utf-8','utf-8',$data);
						}else if(stripos($data,'charset=gb2312',$charset_pos)) {
							$data = iconv('gb2312','utf-8',$data);
						}else if(stripos($data,'charset=gbk',$charset_pos)) {
							$data = iconv('gbk','utf-8',$data);
						}
					}
					return $data;
				}
				private function match_links($url) {
					$data = $this->get_data($url);
					$pattern = '/<h3 class=\"title\"><a(.*?)href="(.*?)">(.*?)<\/a><\/h3>/';
					preg_match_all($pattern, $data, $m); 
					return $m[2];
				}
	
				private function get_dz_title($data) {
					$pattern = '/<h1 class=\"title\">(.*?)<\/h1>/';
					preg_match_all($pattern, $data, $m); 
					return $m[1];
				}
				private function get_dz_context($data) {//需要判断是否是图片，图片存图片的url
					if(preg_match_all('/<p><img title=(.*?)<\/p>/', $data, $hit)){
						return false;
					}
					else
					{
						$pattern = '/<div class=\"cont\"><p>(.*?)<\/p><p style=\"display:none;\"/';
						preg_match_all($pattern, $data, $m); 
						$count = count($m[1]);
						for($i=0;$i<$count;$i++){//删除文字内容中的html标签
							$m[1][$i]=strip_tags($m[1][$i]);
						}
						return $m[1];
					}
				}
				
				private function check($title)
				{
					$this->load->database();
					$this->db->select('id');
					$this->db->where('title',$title);
					$query = $this->db->get('context');
					if($query->num_rows() > 0)//url已存在
					{
						return FALSE;
					}else{  //url不存在
						return TRUE;
					}	
				}
				
				public function index(){
					$this->load->database();
					$get_url = array();
					$get_url = $this->match_links('http://www.haoduanzi.com/');
					$title = array();
					$content = array();
					$data_json = array();
					foreach($get_url as $url){
						$data = $this->get_data($url);
						if($this->get_dz_context($data)!=false){
							$title[] = $this->get_dz_title($data);
							$content[] = $this->get_dz_context($data);
						}
					}
					$count = count($title);
					for($i = 0;$i<$count;$i++){
						$array = array(
									'title'=>$title[$i][0],
									'content'=>$content[$i][0]
								);
						$result[] = $array;
						if($this->check($array['title'])){
							$this->db->insert('context',$array);
						}
					}
					
				}
				*/
				
				private function get_data($url) {
					if($url !== false){
						$data = file_get_contents( $url );
					}
					$charset_pos = stripos($data,'charset');
					if($charset_pos) {
						if(stripos($data,'charset=utf-8',$charset_pos)) {
							$data = iconv('utf-8','utf-8',$data);
						}else if(stripos($data,'charset=gb2312',$charset_pos)) {
							$data = iconv('gb2312','utf-8',$data);
						}else if(stripos($data,'charset=gbk',$charset_pos)) {
							$data = iconv('gbk','utf-8',$data);
						}
					}
					return $data;
				}
				private function get_nh_context_id($data) {
						 $pattern = '/<p><a style=\" font-size:14px;\" href=\"\/view_(.*?)\" target=\"_blank\">(.*?)<\/a><\/p>/';

						$data=preg_replace("/\s+/", " ", $data);
						preg_match_all($pattern, $data, $m); 
						//var_dump($m[2]);
						$count = count($m[2]);
						for($i=0;$i<$count;$i++){//删除文字内容中的html标签
							$m[2][$i]=strip_tags($m[2][$i]);
						}
					//	var_dump($m[2]);
						return $m;
				}
				  private function get_nh_context_author($data) {
                                                $pattern = '/<p style=\"color:#676767;\">(.*?)&nbsp/';
                                                $data=preg_replace("/\s+/", " ", $data);
                                                preg_match_all($pattern, $data, $m);
                                                //var_dump($m[2]);
                                                $count = count($m[1]);
                                                for($i=0;$i<$count;$i++){//删除文字内容中的html标签
                                                        $m[1][$i]=strip_tags($m[1][$i]);
                                                }
                                        //      var_dump($m[2]);
                                                return $m;
                                }
			 private function get_shici($data1) {
                                                $pattern = '/<p style=\"margin:0px; font-size:12px;line-height:160%;\"><span>原文：<\/span><\/p>(.*?)<\/div>/';

                                                $data1=preg_replace("/\s+/", " ", $data1);
                                                preg_match_all($pattern, $data1, $m);
                                                //var_dump($m[2]);
                                                $count = count($m[1]);
                                                for($i=0;$i<$count;$i++){//删除文字内容中的html标签
                                                        $m[1][$i]=strip_tags($m[1][$i]);
                                                }
					//	var_dump($m[1]);
                                                return $m;
                                }	
				private function check($url)
				{
					$this->load->database();
					$this->db->select('id');
					$this->db->where('url',$url);
					$query = $this->db->get('tangdai');
					if($query->num_rows() > 0)//content_id已存在
					{
						return FALSE;
					}else{  //content_id不存在
						return TRUE;
					}	
				}
				
	/*********************************nh**********************************/
				public function index(){
					$this->load->database();
					$content = array();
					$shici = array();
					$author = array();
					 for($m = 1;$m<201;$m++){
					$data = $this->get_data('http://so.gushiwen.org/type.aspx?p='.$m.'&c=%E5%94%90%E4%BB%A3');
					$content= $this->get_nh_context_id($data);
					$author = $this->get_nh_context_author($data);
					$count = count($content[1]);
					for($i = 0;$i<$count;$i++){
						$data1 = $this->get_data('http://so.gushiwen.org/view_'.$content[1][$i]);
						$shici = $this->get_shici($data1);
						
					$array = array('url'=>$content[1][$i],			
								'author'=>$author[1][0],	
								'title'=>$content[2][$i],
								'shiwen'=>$shici[1][0]	
								);
						if($this->check($array['url'])){
							$this->db->insert('tangdai',$array);
						}
					}
					
				  }
				}
			}
